package cn.iocoder.yudao.module.train.controller.admin.coursestudentregistform;

import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.CourseStudentRegistFormDTO;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.train.controller.admin.coursestudentregistform.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.coursestudentregistform.CourseStudentRegistFormDO;
import cn.iocoder.yudao.module.train.service.coursestudentregistform.CourseStudentRegistFormService;

@Tag(name = "管理后台 - 班级学生")
@RestController
@RequestMapping("/train/course-student-regist-form")
@Validated
public class CourseStudentRegistFormController {

    @Resource
    private CourseStudentRegistFormService courseStudentRegistFormService;

    @PostMapping("/create")
    @Operation(summary = "创建班级学生")
    @PreAuthorize("@ss.hasPermission('train:course-student-regist-form:create')")
    public CommonResult<Integer> createCourseStudentRegistForm(@Valid @RequestBody CourseStudentRegistFormSaveReqVO createReqVO) {
        return success(courseStudentRegistFormService.createCourseStudentRegistForm(createReqVO));
    }

    @PostMapping("/create-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量创建班级学生")
    @PreAuthorize("@ss.hasPermission('train:course-student-regist-form:create')")
    public CommonResult<Boolean> createCourseStudentRegistFormList(@RequestParam("classesId") Long classesId, @RequestParam("ids") List<Integer> ids) {
        courseStudentRegistFormService.createCourseStudentRegistFormByIds(classesId, ids);
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新班级学生")
    @PreAuthorize("@ss.hasPermission('train:course-student-regist-form:update')")
    public CommonResult<Boolean> updateCourseStudentRegistForm(@Valid @RequestBody CourseStudentRegistFormSaveReqVO updateReqVO) {
        courseStudentRegistFormService.updateCourseStudentRegistForm(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除班级学生")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('train:course-student-regist-form:delete')")
    public CommonResult<Boolean> deleteCourseStudentRegistForm(@RequestParam("id") Integer id) {
        courseStudentRegistFormService.deleteCourseStudentRegistForm(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除班级学生")
                @PreAuthorize("@ss.hasPermission('train:course-student-regist-form:delete')")
    public CommonResult<Boolean> deleteCourseStudentRegistFormList(@RequestParam("ids") List<Integer> ids) {
        courseStudentRegistFormService.deleteCourseStudentRegistFormListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得班级学生")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('train:course-student-regist-form:query')")
    public CommonResult<CourseStudentRegistFormRespVO> getCourseStudentRegistForm(@RequestParam("id") Integer id) {
        CourseStudentRegistFormDO courseStudentRegistForm = courseStudentRegistFormService.getCourseStudentRegistForm(id);
        return success(BeanUtils.toBean(courseStudentRegistForm, CourseStudentRegistFormRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得班级学生分页")
    @PreAuthorize("@ss.hasPermission('train:course-student-regist-form:query')")
    public CommonResult<PageResult<CourseStudentRegistFormRespVO>> getCourseStudentRegistFormPage(@Valid CourseStudentRegistFormPageReqVO pageReqVO) {
        PageResult<CourseStudentRegistFormDTO> pageResult = courseStudentRegistFormService.getCourseStudentRegistFormPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CourseStudentRegistFormRespVO.class));
    }

    @GetMapping("/listByClassesId")
    @Operation(summary = "获得班级学生列表")
    @PreAuthorize("@ss.hasPermission('train:course-student-regist-form:query')")
    public CommonResult<List<CourseStudentRegistFormRespVO>> getCourseStudentRegistFormList(@RequestParam("classesId") Long classesId) {
        List<CourseStudentRegistFormDTO> listResult = courseStudentRegistFormService.getCourseStudentRegistFormListByClassesId(classesId);
        return success(BeanUtils.toBean(listResult, CourseStudentRegistFormRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出班级学生 Excel")
    @PreAuthorize("@ss.hasPermission('train:course-student-regist-form:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCourseStudentRegistFormExcel(@Valid CourseStudentRegistFormPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CourseStudentRegistFormDTO> list = courseStudentRegistFormService.getCourseStudentRegistFormPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "班级学生.xls", "数据", CourseStudentRegistFormRespVO.class,
                        BeanUtils.toBean(list, CourseStudentRegistFormRespVO.class));
    }

}
